package com.headwind.logistics.common.config;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
@Controller
public class ErrorConfig implements ErrorController {

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request, Model model){
        //获取statusCode:401,404,500
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");


        if(statusCode == 401){

            return "/error/404";
        }else if(statusCode == 404){
            return "/error/404";
        }else if(statusCode == 403){
            return "/error/403";
        }else{
            if (exception != null){
                model.addAttribute("EXCEPTION",exception.getMessage());
            }
            return "/error/500";
        }


    }

    @Override
    public String getErrorPath() {
        return null;
    }
}
